#!/bin/bash

OK=0
WARN=1
CRITICAL=2
UNKNOWN=3
PATH="/bin:/sbin:/usr/bin:/usr/sbin"

Warn_temp=80
Shutdown_temp=95


Min_cpu_temp=`/usr/bin/sensors | grep -E 'Core|temp1'| cut -d+ -f2 | cut -c1-2 |sort -n  |head -n 1`

if [ "T$Min_cpu_temp" == "T" ]  #Judge sensor is correctly 
then
        echo "sensor Can not correctly fetch the cpu temp"
        exit $CRITICAL
fi


if [ $(echo $Min_cpu_temp/1|bc) != "$Min_cpu_temp" ]  #judge the temp value is Integer
then
	echo "Can not correctly fetch the cpu temp "
	exit $CRITICAL
fi


if [ $Min_cpu_temp -gt $Shutdown_temp ]
then
	echo "cpu min temp now is $Min_cpu_temp higher than Shutdown_temp $Shutdown_temp, exec shutdown command|CpuTemp=$Min_cpu_temp;$Warn_temp;$Shutdown_temp;;"
        echo "o" > /proc/sysrq-trigger   ####exec shutdown comman
	#echo "exec shutdown command"
	exit $CRITICAL
elif [ $Min_cpu_temp -gt $Warn_temp ]
then
	echo "cpu min temp now is $Min_cpu_temp exceed warning temp $Warn_temp but lower than Shutdown_temp $Shutdown_temp|CpuTemp=$Min_cpu_temp;$Warn_temp;$Shutdown_temp;;"
	exit $WARN
else
	echo "cpu core min temp now is $Min_cpu_temp lower than warning temp $Warn_temp and Shutdown_temp $Shutdown_temp|CpuTemp=$Min_cpu_temp;$Warn_temp;$Shutdown_temp;;"
	exit $OK
fi
